$(document).ready(function() {
    $('#map').height($('.page-container').height());
    $(window).resize(function() {
        $('#map').height($('.page-container').height());
    });


 if(MAP_DEFAULT_LAYOUT == 'SATELLITE'){
            var typeId = google.maps.MapTypeId.SATELLITE;
        }else{
            var typeId = google.maps.MapTypeId.ROADMAP;
        }
        
    var infobox;
    var myLatlng = new google.maps.LatLng(DefaultLat, DefaultLong);
    var mapOptions = {
        zoom: 10,
        center: myLatlng,
        zoomControl: false,
        zoomControlOpt: {
            style: 'SMALL',
            position: 'TOP_LEFT'
        },
        panControl: false,
        streetViewControl: false,
        mapTypeId: typeId,
        mapTypeControl:true,
        mapTypeControlOptions: {
          //  style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
            position: google.maps.ControlPosition.TOP_LEFT
        },
        //mapTypeId: google.maps.MapTypeId.SATELLITE,
        overviewMapControl: false,
        
    }
    var map = new google.maps.Map(document.getElementById('map'), mapOptions);
    var bounds = new google.maps.LatLngBounds();
    /*var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        animation: google.maps.Animation.DROP,
        title: 'Hello World!',
    });
    marker.setMap(map);
    */
   
        
    var markers = JSON.parse(MapLatlong);
     var gmarkers = [];
    //alert(markers.length);
    if (markers.length != '0') {
        var decoded = $('<div/>').html(WindowData).text();
        
        var infoWindowContent = JSON.parse(decoded);
        var MarkerData = JSON.parse(MarkerContent);
        var MapColor = JSON.parse(map_color);
        // Display multiple markers on a map
        var infoWindow = new google.maps.InfoWindow(), marker, i;
        for (i = 0; i < markers.length; i++) {
            var value = infoWindowContent[i][0]; 
       /*     var styleMaker2 = new StyledMarker(
                    {
                styleIcon:new StyledIcon(StyledIconTypes.BUBBLE,{
                    color:"1890B5",
                    text:infoWindowContent[i][0]
                }),
                position:new google.maps.LatLng(markers[i][1], markers[i][2]),
                map:map
            });
	*/
            var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
            bounds.extend(position);
          /*  marker = new google.maps.Marker({
                position: position,
                map: map,
                title: markers[i][0],
                
            });*/
             var marker = new StyledMarker({
                styleIcon: new StyledIcon(StyledIconTypes.BUBBLE,{color:MapColor[i][0], text:MarkerData[i][0]}),
                position: position,
                map: map,
            });
            gmarkers.push(marker);
            infobox = new InfoBox({
                 maxWidth: 280,
                 zIndex: null,
                 boxStyle: {
               //  width: "280px",
                },
                closeBoxURL: BASE_PATH + "public/img/mapclose.png",
                infoBoxClearance: new google.maps.Size(1, 1),
            });
    
    
            // Allow each marker to have an info window    
            google.maps.event.addListener(marker, 'click', (function(styleMaker2, i) {
                return function() {
                 infobox.open(map, marker);
                //   map.panTo(loc);
                 infobox.setContent(infoWindowContent[i][0]); 
                    //infoWindow.setContent(infoWindowContent[i][0]);
                    //infoWindow.open(map, marker);
                }
            })(marker, i));

/*
            google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
                return function() {
                   infobox.open(map, marker);
                   infobox.setContent(infoWindowContent[i][0]); 
//infoWindow.setContent(infoWindowContent[i][0]);
                    //infoWindow.open(map, marker);
                }
            })(marker, i));
*/
            // Automatically center the map fitting all markers on the screen
            map.fitBounds(bounds);
        }
    }
    /*End multiple marker*/

    $("#map-zoom-out").click(function() {
        var currentZoom = map.getZoom();
        map.setZoom(currentZoom - 1);
    });

    $("#map-zoom-in").click(function() {
        var currentZoom = map.getZoom();
        map.setZoom(currentZoom + 1);
    });



 $(".map_search").click(function() {
        var type_chk = $(this).attr('type_name');
        var chkArray = [];
        var property_sale_all_array = [];
        var property_sale_published_array = [];
        var property_sale_unpublished_array = [];

        var property_sale_all_subtype_array = [];
        var property_sale_published_subtype_array = [];
        var property_sale_unpublished_subtype_array = [];

     
     
        var property_rent_all_array = [];
        var property_rent_published_array = [];
        var property_rent_unpublished_array = [];
        
        
        var property_rent_all_subtype_array = [];
        var property_rent_published_subtype_array = [];
        var property_rent_unpublished_subtype_array = [];
        
        
        var map_category = [];
        
        $(".map_cat_class:checked").each(function() {
            map_category.push($(this).val());
        });
        var selected_map;
        selected_map = map_category.join(',');
        var map_loc_category = selected_map;
        
        
        
        $(".valuation_class:checked").each(function() {
            chkArray.push($(this).val());
        });
        var selected;
        selected = chkArray.join(',');
        var valuation_category = selected;


        $(".property_sale_all:checked").each(function() {
            property_sale_all_array.push($(this).val());
        });
        var property_sale_all_selected;
        property_sale_all_selected = property_sale_all_array.join(',');
        var property_sale_all = property_sale_all_selected;


        $(".property_sale_all_subtype:checked").each(function() {
            property_sale_all_subtype_array.push($(this).val());
        });
        var property_sale_all_subtype_selected;
        property_sale_all_subtype_selected = property_sale_all_subtype_array.join(',');
        var property_sale_all_subtype = property_sale_all_subtype_selected;


        $(".property_sale_published:checked").each(function() {
            property_sale_published_array.push($(this).val());
        });
        var property_sale_published_selected;
        property_sale_published_selected = property_sale_published_array.join(',');
        var property_sale_published = property_sale_published_selected;


        $(".property_sale_published_subtype:checked").each(function() {
            property_sale_published_subtype_array.push($(this).val());
        });
        var property_sale_published_subtype_selected;
        property_sale_published_subtype_selected = property_sale_published_subtype_array.join(',');
        var property_sale_published_subtype = property_sale_published_subtype_selected;



        $(".property_sale_unpublished:checked").each(function() {
            property_sale_unpublished_array.push($(this).val());
        });
        var property_sale_unpublished_selected;
        property_sale_unpublished_selected = property_sale_unpublished_array.join(',');
        var property_sale_unpublished = property_sale_unpublished_selected;

        $(".property_sale_unpublished_subtype:checked").each(function() {
            property_sale_unpublished_subtype_array.push($(this).val());
        });
        var property_sale_unpublished_subtype_selected;
        property_sale_unpublished_subtype_selected = property_sale_unpublished_subtype_array.join(',');
        var property_sale_unpublished_subtype = property_sale_unpublished_subtype_selected;



        $(".property_rent_all:checked").each(function() {
            property_rent_all_array.push($(this).val());
        });
        var property_rent_all_selected;
        property_rent_all_selected = property_rent_all_array.join(',');
        var property_rent_all = property_rent_all_selected;


        $(".property_rent_published:checked").each(function() {
            property_rent_published_array.push($(this).val());
        });
        var property_rent_published_selected;
        property_rent_published_selected = property_rent_published_array.join(',');
        var property_rent_published = property_rent_published_selected;


        $(".property_rent_unpublished:checked").each(function() {
            property_rent_unpublished_array.push($(this).val());
        });
        var property_rent_unpublished_selected;
        property_rent_unpublished_selected = property_rent_unpublished_array.join(',');
        var property_rent_unpublished = property_rent_unpublished_selected;


$(".property_rent_all_subtype:checked").each(function() {
            property_rent_all_subtype_array.push($(this).val());
        });
        var property_rent_all_subtype_selected;
        property_rent_all_subtype_selected = property_rent_all_subtype_array.join(',');
        var property_rent_all_subtype = property_rent_all_subtype_selected;


        $(".property_rent_published_subtype:checked").each(function() {
            property_rent_published_subtype_array.push($(this).val());
        });
        var property_rent_published_subtype_selected;
        property_rent_published_subtype_selected = property_rent_published_subtype_array.join(',');
        var property_rent_published_subtype = property_rent_published_subtype_selected;


        $(".property_rent_unpublished_subtype:checked").each(function() {
            property_rent_unpublished_subtype_array.push($(this).val());
        });
        var property_rent_unpublished_subtype_selected;
        property_rent_unpublished_subtype_selected = property_rent_unpublished_subtype_array.join(',');
        var property_rent_unpublished_subtype = property_rent_unpublished_subtype_selected;




        var postdata = {};
        var obj = $(this);
        postdata['map_loc_category'] = map_loc_category;
        postdata['valuation_category'] = valuation_category;
        postdata['property_sale_all'] = property_sale_all;
        postdata['property_sale_published'] = property_sale_published;
        postdata['property_sale_unpublished'] = property_sale_unpublished;
        
        postdata['property_sale_all_subtype'] = property_sale_all_subtype;
        postdata['property_sale_published_subtype'] = property_sale_published_subtype;
        postdata['property_sale_unpublished_subtype'] = property_sale_unpublished_subtype;
        
        
        postdata['property_rent_all'] = property_rent_all;
        postdata['property_rent_published'] = property_rent_published;
        postdata['property_rent_unpublished'] = property_rent_unpublished;
        
        postdata['property_rent_all_subtype'] = property_rent_all_subtype;
        postdata['property_rent_published_subtype'] = property_rent_published_subtype;
        postdata['property_rent_unpublished_subtype'] = property_rent_unpublished_subtype;


        jQuery.post(BASE_PATH + 'maplocation/searchmapdata', postdata, function(result) {
                var jsonarray = JSON.parse(result); 
                var MapLatlong = jsonarray['mapDataLatLong'];
                var WindowData = jsonarray['infoWindowContent'];
                var MarkerData = jsonarray['MarkerContent'];
                var MapColor = jsonarray['MapColor'];
                var map_icon = jsonarray['ma_icon'];
                //console.log(map_icon);
                for (i = 0; i < gmarkers.length; i++) {
                 //markers[i].setMap(null);
                  gmarkers[i].setMap(null);
            }
            
            var markers = MapLatlong;
            
            if (markers.length != '0') {
           // var decoded = $('<div/>').html(WindowData).text();
            var infoWindowContent = WindowData;
            // Display multiple markers on a map
            var infoWindow = new google.maps.InfoWindow(), marker, i;
            
            
            
            
            for (i = 0; i < markers.length; i++) {
                //var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
                var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
                bounds.extend(position);
              /*  marker = new google.maps.Marker({
                    position: position,
                    map: map,
                    title: markers[i][0],
                   // icon: map_icon[i]
                   
                });
                */
                 var marker = new StyledMarker({
                    styleIcon: new StyledIcon(StyledIconTypes.BUBBLE,{color:MapColor[i][0], text:MarkerData[i][0]}),
                    position: position,
                    map: map,
                });
                
                gmarkers.push(marker);

                // Allow each marker to have an info window    
                google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        infobox.open(map, marker);
                        infobox.setContent(infoWindowContent[i][0]); //infoWindow.setContent(infoWindowContent[i][0]);
                        //infoWindow.open(map, marker);
                    }
                })(marker, i));


/*
                google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
                    return function() {
                        infobox.open(map, marker);
                   infobox.setContent(infoWindowContent[i][0]); //infoWindow.setContent(infoWindowContent[i][0]);
                        //infoWindow.open(map, marker);
                    }
                })(marker, i));
                */
             
                // Automatically center the map fitting all markers on the screen
                map.fitBounds(bounds);
            }
        }
        /*End multiple marker*/
            
            });
    });





});